package cn.yanjr.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;

import cn.yanjr.common.dao.BaseDaoImpl;

@WebFilter("/*")
public class BaseFilter implements Filter {
	
	private Logger logger = Logger.getLogger(BaseFilter.class);
	
	/**
	 * 过滤拦截
	 */
	public static String[] ALLOW_PATH = {"/static/*"};

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        
        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
        resp.setHeader("Access-Control-Max-Age", "3600");
        resp.setHeader("Access-Control-Allow-Headers", "*");
//        resp.setHeader("Access-Control-Allow-Credentials", "true"); 
        
        if ("OPTIONS".equals(req.getMethod())) {
        	resp.setStatus(HttpServletResponse.SC_OK);
            return;
        }
        
        response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		
        
        String countextPath = req.getContextPath();
        String servletPath = req.getServletPath();
        String url = req.getRequestURI();
        
        if(isAllow(servletPath)) {
        	chain.doFilter(request, response);
        } else {
        	logger.info("BaseFilter doFilter() countextPath==>" + countextPath);
        	logger.info("BaseFilter doFilter() servletPath==>" + servletPath);
        	logger.info("BaseFilter doFilter() url==>" + url);
    		
    		if(servletPath.endsWith("/")) {
    			servletPath = servletPath.substring(0, servletPath.length() -1);
    		}
    		
    		String[] pathSpilt = servletPath.split("/");
    		
    		chain.doFilter(request, response);
        }
        
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}
	
	/**
	 * 允许访问
	 * @Title: isAllow
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param servletPath
	 * @return boolean
	 * 
	 * @author: yanjr  big123@126.com
	 * @date:   2020年3月6日 下午5:16:40
	 */
	public boolean isAllow(String servletPath) {
		boolean isAllow = false;
		if(ALLOW_PATH != null && ALLOW_PATH.length > 0) {
			for(String path : ALLOW_PATH) {
				if(path.endsWith("*")) {
					path = path.replace("*", "");
					if(servletPath.startsWith(path)) {
						isAllow = true;
					}
				} else {
					if(path.equals(servletPath)) {
						isAllow = true;
					}
				}
			}
		}
		
		
		return isAllow;
	}

}
